package com.renwei.yunlong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.OwnerSelectNewAdapter;
import com.renwei.yunlong.adapter.ServiceSelectNewAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.OwnerSelectNewBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.renwei.yunlong.utils.ToastUtil;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择服务对象--页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class SelectServiceNewActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_scroll)
    ImageView ivScroll;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.rl_search)
    RelativeLayout rlSearch;
    @Bind(R.id.et_search)
    EditText etSearch;

    private ACache mCache;

    private OwnerSelectNewAdapter ownerAdapter;
    private ServiceSelectNewAdapter serviceAdapter;

    String search;//搜索
    String companyType;//分类：业主、服务商
    String companyCode;//公司code
    String contractId;
    LoginBean loginBean;
    ServiceLoginBean serviceLoginBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_select_service_new);
        ButterKnife.bind(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        mCache = ACache.get(this);
        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("选择服务对象");
        ivScroll.setVisibility(View.VISIBLE);
        ivScroll.setOnClickListener(this);

        rlSearch.setOnClickListener(this);

        search = etSearch.getText().toString().trim();
        companyType = mCache.getAsString("companyType");
        companyCode = getIntent().getStringExtra("companyCode");
        contractId = getIntent().getStringExtra("contractId");
        loginBean = (LoginBean) mCache.getAsObject("loginBean");
        serviceLoginBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");

        if ("1".equals(companyType)) {//业主
            //{"currentUserId":当前员工employeeId ,"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":1}
            if (loginBean != null){
                    String jsonBean = "{currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" +loginBean.getRows().getOwnerCode() + "\",hardName:\"" + "" +"\",typeStatus:\"" + "1" + "\"}";
                    Log.e("业主Json",jsonBean);
                    ownerSelectNewUnitHttp(jsonBean);
            }
        } else if ("2".equals(companyType)) {//服务商
            //{"ownerCode":选择的发起企业Code ," contractId ":项目ID,"currentUserId":当前员工employeeId," serviceProviderCode ":当前公司编码,"hardName":此页面查询条件, hardSerial:序列号查询，"typeStatus":1}
            if (!TextUtils.isEmpty(companyCode)) {
                String jsonBean = "{ownerCode:\"" + companyCode + "\",contractId:\"" + contractId + "\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardName:\"" +""+"\",typeStatus:\"" + "1" + "\"}";
                Log.e("服务商Json",jsonBean);
                serviceSelectNewUnitHttp(jsonBean);
            }else {

            }
        }
    }

    private void setOwnerListDate(List<OwnerSelectNewBean.Rows> mList) {

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        ownerAdapter = new OwnerSelectNewAdapter(SelectServiceNewActivity.this, mList);
        ownerAdapter.setOnItemClickListener(new OwnerSelectNewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name,String ownerHardwareId) {
                Intent intent = new Intent();
                intent.putExtra("name",name);
                mCache.put("ownerHardwareId",ownerHardwareId);
                setResult(RESULT_OK, intent);
                SelectServiceNewActivity.this.finish();
            }
        });
        recyclerView.setAdapter(ownerAdapter);
    }

    private void setServiceListDate(List<OwnerSelectNewBean.Rows> mList) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        serviceAdapter = new ServiceSelectNewAdapter(SelectServiceNewActivity.this,mList);
        serviceAdapter.setOnItemClickListener(new ServiceSelectNewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String name,String hardwareId) {
                Intent intent = new Intent();
                intent.putExtra("name",name);
                mCache.put("serviceHardwareId",hardwareId);
                setResult(RESULT_OK, intent);
                SelectServiceNewActivity.this.finish();
            }
        });
        recyclerView.setAdapter(serviceAdapter);
    }

    //业主工单服务对象
    private void ownerSelectNewUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectServiceNewActivity.this);
        hp.setUrl(Api.OWNER_QUERYSEARCH_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商工单服务对象
    private void serviceSelectNewUnitHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(SelectServiceNewActivity.this);
        hp.setUrl(Api.SERVICE_QUERYHARD_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_scroll://扫描
                //扫二维码 获取 序列号查询  342342343432-98

                break;
            case R.id.rl_search:
                if ("1".equals(companyType)) {//业主
                    //{"currentUserId":当前员工employeeId ,"ownerCode":当前公司编码,"hardName":此页面查询条件,"typeStatus":1}
                    if (loginBean != null){
                        if (!TextUtils.isEmpty(search)){
                            String jsonBean = "{currentUserId:\"" + loginBean.getRows().getEmployeeId() + "\",ownerCode:\"" +loginBean.getRows().getOwnerCode() + "\",hardName:\"" + search +"\",typeStatus:\"" + "1" + "\"}";
                            Log.e("业主Json",jsonBean);
                            ownerSelectNewUnitHttp(jsonBean);
                        }
                    }
                } else if ("2".equals(companyType)) {//服务商
                    //{"ownerCode":选择的发起企业Code ," contractId ":项目ID,"currentUserId":当前员工employeeId," serviceProviderCode ":当前公司编码,"hardName":此页面查询条件, hardSerial:序列号查询，"typeStatus":1}
                    if (!TextUtils.isEmpty(companyCode)) {
                        if (!TextUtils.isEmpty(search)){
                            String jsonBean = "{ownerCode:\"" + companyCode + "\",contractId:\"" + contractId + "\",currentUserId:\"" + serviceLoginBean.getRows().getEmployeeId() + "\",serviceProviderCode:\"" + serviceLoginBean.getRows().getServiceProviderCode() + "\",hardName:\"" +search+"\",typeStatus:\"" + "1" + "\"}";
                            Log.e("服务商Json",jsonBean);
                            serviceSelectNewUnitHttp(jsonBean);
                        }
                    }else {

                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主返回",result);
                OwnerSelectNewBean ownerBean = new Gson().fromJson(result,OwnerSelectNewBean.class);
                if (ownerBean.getMessage().getCode() == 200){
                    setOwnerListDate(ownerBean.getRows());
                }
                break;
            case 2:
                Log.e("服务商返回",result);
                OwnerSelectNewBean ownerSelectNewBean = new Gson().fromJson(result,OwnerSelectNewBean.class);
                if (ownerSelectNewBean.getMessage().getCode() == 200){
                    setServiceListDate(ownerSelectNewBean.getRows());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }
}
